﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMIS.Entities;
using DX.CRC.Common.Entities;

namespace SMIS.Dals
{
    class Dal4Job : DalBase
    {
        public static List<Job> SearchAllJobs()
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"SELECT [JobID] as ID
                          ,[JobName] as Name
                          ,[CreateTime]
                          ,[IsActive]
                          ,[Remark] as Des
                      FROM [JobInfo] WHERE IsActive=1";

            return qo.xExecuteList<Job>();
        }

        /// <summary>
        /// 【闻祖东 2013-4-8-193727】将多个参数进行面向对象的封装。
        /// 到了这个阶段，可以在写代码的时候想一下自己写的代码的规律，是否有很多地方有相似之处？
        /// 如果有，那么是否可以进行某种封装？
        /// </summary>
        /// <param name="name"></param>
        /// <param name="remark"></param>
        public static void InsertJob(string name, string remark)
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"INSERT INTO [JobInfo]
                           ([JobName]
                           ,[CreateTime]
                           ,[IsActive]
                           ,[Remark])
                     VALUES
                           (@JobName
                           ,GETUTCDATE()
                           ,1
                           ,@Remark)";

            qo.AddParam("@JobName", name);
            qo.AddParam("@Remark", remark);

            qo.xExecuteNonQuery();
        }

        public static bool UpdateSelectedJob(Job job)
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"declare @Count int
                           select @Count=COUNT(*) from [JobInfo] 
                           where @JobName in (select JobName from JobInfo where JobID<>@JobID)
                           if @Count=0
                           begin
                           UPDATE [JobInfo]
                               SET [JobName] = @JobName
                                  ,[Remark] = @Remark
                           WHERE JobID=@JobID
                           end";
            qo.AddParam("@JobID", job.ID);
            qo.AddParam("@JobName", job.Name);
            qo.AddParam("@Remark", job.Des);

            return qo.xExecuteNonQuery()==1;
        }

        public static bool DeleteSelectedJob(Job job)
        {
            QueryObject qo = CreateQO();
            qo.CmdText = @"declare @Count int
                           select @Count=COUNT(*) from JobInfo J 
                           where @JobName in (select S.JobName from StaffInfo S)
                           if @Count=0
                           begin
                           UPDATE [JobInfo]
                           SET [IsActive] = 0
                           WHERE JobID=@JobID
                           end";

            qo.AddParam("@JobID",job.ID);
            qo.AddParam("@JobName",job.Name);

            return qo.xExecuteNonQuery() == 1;
        }
    }
}
